package com.niannian.activity;

import java.io.File;
import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.niannian.util.Common;
import com.niannian.util.PlayerManager;
import com.niannian.util.RecordManager;
import com.niannian.util.VAR;
import com.niannian.BaseActivity;
import com.niannian.MyApplication;
import com.niannian.R;

/**
 * 发送语音
 * 
 * @author shengl
 * 
 */
public class RecordActivity extends BaseActivity {

	public static final int FROM_COMMENT = 1;// 表示来自评论

	private ImageView iv_volume_show;

	TextView tv_aud_len;
	RelativeLayout btn_play;
	ImageView iv_status_play;

	ImageView iv_record;
	TextView tv_afresh_record;
	TextView tv_record_tip_2;

	private TextView tv_record_time_tip, tv_record_time_remaintip, tv_record_tip_3;

	RecordManager rm;
	String savePath = "";// 语音路径
	int from_type = 0;// 0表示来自发布界面，1表示来自评论,3表示来自HOME
	File file;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_record);

		from_type = getIntent().getIntExtra("from", 0);
		initView();
		setLister();
		initTopView();

	}

	// 头部视图
	private ImageView iv_top_left, iv_top_right;
	private TextView top_title;
	private LinearLayout iv_top_left2, top_title2;

	private void initTopView() {
		iv_top_left = (ImageView) findViewById(R.id.iv_top_left);
		iv_top_left2 = (LinearLayout) findViewById(R.id.iv_top_left2);
		iv_top_left.setVisibility(View.VISIBLE);
		iv_top_left2.setVisibility(View.GONE);

		top_title = (TextView) findViewById(R.id.top_title);
		top_title2 = (LinearLayout) findViewById(R.id.top_title2);
		top_title.setVisibility(View.VISIBLE);
		top_title2.setVisibility(View.GONE);
		top_title.setText("录音");

		iv_top_right = (ImageView) findViewById(R.id.iv_top_right);
		iv_top_right.setVisibility(View.GONE);
		iv_top_right.setImageResource(R.drawable.next_step_e);

		iv_top_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				myfinish();
			}
		});
		iv_top_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (timeCount > 0) {

					Intent intent = new Intent();
					intent.putExtra("time", timeCount);
					intent.putExtra("savePath", savePath);
					setResult(RESULT_OK, intent);
					myfinish();
				}
			}
		});
	}

	private void initView() {
		iv_volume_show = (ImageView) findViewById(R.id.iv_volume_show);

		tv_aud_len = (TextView) findViewById(R.id.tv_aud_len);
		btn_play = (RelativeLayout) findViewById(R.id.btn_play);
		iv_status_play = (ImageView) findViewById(R.id.iv_status_play);

		tv_record_time_remaintip = (TextView) findViewById(R.id.tv_record_time_remaintip);
		tv_record_time_tip = (TextView) findViewById(R.id.tv_record_time_tip);
		tv_record_tip_3 = (TextView) findViewById(R.id.tv_record_tip_3);

		iv_record = (ImageView) findViewById(R.id.iv_record);
		tv_afresh_record = (TextView) findViewById(R.id.tv_afresh_record);
		tv_record_tip_2 = (TextView) findViewById(R.id.tv_record_tip_2);

		PlayerManager.getAudioPlayer(RecordActivity.this).playerPause();
		RecordView();
		if (timeHandler == null) {
			listenerTime();
		}

		rm = new RecordManager(iv_volume_show);
		rm.listenerTime(timeHandler);
		savePath = MyApplication.FILE_PATH_TEMP + ((new Date()).getTime() / 1000) + ".amr";
		file = new File(savePath);
		tv_record_tip_2.setText("手指上滑，取消录音");
		tv_record_tip_2.setTextColor(getResources().getColor(R.color.cb10));
		tv_record_tip_2.setVisibility(View.INVISIBLE);
		tv_record_time_tip.setText("00:00");
		timeCount = 0;

	}

	Boolean isRecord = false;
	float time_tipy = 0;
	float deltaY = 0;

	private void setLister() {

		iv_record.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {

					// if (System.currentTimeMillis() - VAR.ExitTime >
					// VAR.rINTERVAL) {
					timeHandler.removeMessages(2);
					timeHandler.sendEmptyMessageDelayed(2, 100);
					// }else {
					//
					// }
					// VAR.ExitTime=System.currentTimeMillis();
				} else if (event.getAction() == MotionEvent.ACTION_MOVE && is_ACTION_MOVE) {
					deltaY = event.getRawY();

					if (deltaY < time_tipy) {
						tv_record_tip_2.setText("取消录音");
						tv_record_tip_2.setTextColor(getResources().getColor(R.color.red));
					} else {
						tv_record_tip_2.setText("手指上滑，取消录音");
						tv_record_tip_2.setTextColor(getResources().getColor(R.color.cb10));
					}

				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					iv_record.setImageResource(R.drawable.recording);
					timeHandler.removeMessages(4);
					timeHandler.sendEmptyMessageDelayed(4, 200);

				}
				return true;
			}
		});

		tv_afresh_record.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				timeCount = 0;
				tv_record_time_tip.setText("00:00");
				iv_top_right.setImageResource(R.drawable.next_step);
				RecordView();
			}
		});

	}

	private void startRecord() {
		RecordView();

		top_title.setText("正在录音...");

		timeCount = 0;
		rm.setSavePath(file);
		rm.startRecord();
	}

	Handler timeHandler;
	private int timeCount = 0;
	private int MaxTime = 90;
	private int showTime = 60 * 1;
	Boolean is_ACTION_MOVE = false;

	private void listenerTime() {
		timeHandler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 1:
					timeCount++;
					is_ACTION_MOVE = true;
					if (timeCount > showTime) {

						tv_record_time_remaintip.setVisibility(View.VISIBLE);
						tv_record_time_remaintip.setText("(还能说" + (MaxTime - timeCount) + "秒)");
					} else {
						tv_record_time_remaintip.setVisibility(View.GONE);
					}

					int j = timeCount / 60;
					int i = timeCount % 60;

					if (i < 10) {
						tv_record_time_tip.setText("0" + j + ":0" + i);
					} else {
						tv_record_time_tip.setText("0" + j + ":" + i);
					}

					if (timeCount >= MaxTime) {
						endRecord();
					}
					if (timeCount == 0) {
						tv_record_time_tip.setVisibility(View.INVISIBLE);
						tv_record_tip_3.setVisibility(View.VISIBLE);
					} else {
						tv_record_time_tip.setVisibility(View.VISIBLE);
						tv_record_tip_3.setVisibility(View.GONE);
					}
					if (!isRecord) {
						tv_record_time_tip.setText("00:00");
						tv_record_time_tip.setVisibility(View.VISIBLE);
						tv_record_tip_3.setVisibility(View.GONE);
					}
					break;

				case 2:
					iv_record.setImageResource(R.drawable.recording_pre);

					tv_record_tip_2.setVisibility(View.VISIBLE);
					VAR.ExitTime = System.currentTimeMillis();

					RecordView();
					top_title.setText("正在录音...");
					tv_record_time_tip.setVisibility(View.INVISIBLE);
					tv_record_tip_3.setVisibility(View.VISIBLE);
					timeCount = 0;
					timeHandler.removeCallbacks(startRecordThread);
					timeHandler.post(startRecordThread);
					isRecord = true;
					break;
				case 3:

					is_ACTION_MOVE = true;
					break;

				case 4:

					endRecord();
					break;
				}
				super.handleMessage(msg);
			}
		};
	}

	Runnable startRecordThread = new Runnable() {
		// 将要执行的操作写在线程对象的run方法当中
		public void run() {

			rm.setIsRStatus(true);
			rm.setSavePath(file);
			rm.startRecord();
			is_ACTION_MOVE = false;
			timeHandler.removeMessages(3);
			timeHandler.sendEmptyMessageDelayed(3, 500);
		}
	};

	Runnable endRecordThread = new Runnable() {
		// 将要执行的操作写在线程对象的run方法当中
		public void run() {

			rm.stopRecord();
			rm.setIsRStatus(false);
		}
	};

	private void RecordView() {
		iv_volume_show.setVisibility(View.VISIBLE);
		tv_record_time_tip.setVisibility(View.VISIBLE);
		iv_record.setVisibility(View.VISIBLE);

		btn_play.setVisibility(View.GONE);
		tv_afresh_record.setVisibility(View.GONE);

	}

	private void PlayerView() {
		iv_volume_show.setVisibility(View.GONE);
		tv_record_time_tip.setVisibility(View.VISIBLE);
		iv_record.setVisibility(View.GONE);

		btn_play.setVisibility(View.VISIBLE);
		tv_afresh_record.setVisibility(View.VISIBLE);
		tv_aud_len.setText(Common.showAudLen(timeCount));

		PlayerManager.getAudioPlayer(RecordActivity.this).FromAudition(btn_play, iv_status_play, savePath);
	}

	private void endRecord() {
		is_ACTION_MOVE = false;

		// VAR.ExitTime = System.currentTimeMillis();
		iv_record.setImageResource(R.drawable.recording);
		iv_top_right.setImageResource(R.drawable.next_step);

		timeHandler.removeCallbacks(endRecordThread);
		timeHandler.postDelayed(endRecordThread, 100);
		tv_record_tip_3.setVisibility(View.GONE);
		isRecord = false;

		if (deltaY < time_tipy) {
			RecordView();
			tv_record_time_tip.setText("00:00");
			tv_record_time_remaintip.setVisibility(View.GONE);
			timeCount = 0;

			tv_record_tip_2.setVisibility(View.INVISIBLE);
			tv_record_tip_2.setText("手指上滑，取消录音");
			tv_record_tip_2.setTextColor(getResources().getColor(R.color.cb10));
			top_title.setText("录音");
		} else {

			if (timeCount > 0) {
				Bundle bundle = new Bundle();
				bundle.putInt("time", timeCount);
				bundle.putString("savePath", savePath);

				if (from_type == PushRegardsActivity.FROM_MIAN_RECORD) {
					bundle.putInt("from", PushRegardsActivity.FROM_MIAN_RECORD);
					openActivity(PushRegardsActivity.class, bundle);
				} else {
					Intent intent = new Intent();
					intent.putExtra("time", timeCount);
					intent.putExtra("savePath", savePath);
					intent.putExtras(bundle);
					setResult(RESULT_OK, intent);
				}
				myfinish();

			} else {
				RecordView();
				tv_record_time_remaintip.setVisibility(View.GONE);
				tv_record_time_tip.setText("00:00");
				timeCount = 0;

				tv_record_tip_2.setVisibility(View.INVISIBLE);
				tv_record_tip_2.setText("手指上滑，取消录音");
				tv_record_tip_2.setTextColor(getResources().getColor(R.color.cb10));
				top_title.setText("录音");
			}
		}

	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		super.onWindowFocusChanged(hasFocus);

		int[] location = new int[2];
		tv_record_time_tip.getLocationOnScreen(location);
		time_tipy = location[1];
	}
}
